package com.budwk.app.sqfw.services.impl;

import com.budwk.app.sqfw.models.Sqfw_Sms_Msg;
import com.budwk.app.sqfw.services.SqfwSmsMsgService;
import com.budwk.app.util.poiUtils.SmsMsgExcelUtils;
import com.budwk.starter.database.service.BaseServiceImpl;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.ioc.loader.annotation.IocBean;

import java.util.List;

@IocBean(args = {"refer:dao"})
public class SqfwSmsMsgServiceImpl extends BaseServiceImpl<Sqfw_Sms_Msg> implements SqfwSmsMsgService {
    public SqfwSmsMsgServiceImpl(Dao dao) {
        super(dao);
    }


    @Override
    public String downExcel(List<String> ids) {
        Cnd cnd = Cnd.NEW();
        cnd.and("summary_id", "in", ids);
        cnd.orderBy("create_date", "desc");
        List<Sqfw_Sms_Msg> smsMsgList = this.query(cnd);

        return SmsMsgExcelUtils.downExcel(smsMsgList);
    }

    @Override
    public String downExcel(List<String> ids, Boolean isTemplate) {
        if (isTemplate) return SmsMsgExcelUtils.downExcel(null);

        Cnd cnd = Cnd.NEW();
        cnd.and("summary_id", "in", ids);
        cnd.orderBy("create_date", "desc");
        List<Sqfw_Sms_Msg> smsMsgList = this.query(cnd);

        return SmsMsgExcelUtils.downExcel(smsMsgList);
    }

    @Override
    public String downExcel(String sqfwId) {
        String sql = "select ssm.* from shequ.sqfw_sms_msg ssm left join shequ.sqfw_form_summary sfs " +
                "on ssm.summary_id = sfs.id " +
                "where sfs.sqfw_id = '"+sqfwId+"' order by create_date desc";
        List<Sqfw_Sms_Msg> smsMsgList = this.listEntity(Sqls.create(sql));
        return SmsMsgExcelUtils.downExcel(smsMsgList);
    }

    @Override
    public int dataCheck(List<String> ids) {
        Cnd cnd = Cnd.NEW();
        cnd.and("summary_id", "in", ids);
        cnd.orderBy("create_date", "desc");
        List<Sqfw_Sms_Msg> smsMsgList = this.query(cnd);
        if (smsMsgList == null) return 0;
        return smsMsgList.size();
    }

    @Override
    public int dataCheck(String sqfwId) {
        String sql = "select ssm.id from shequ.sqfw_sms_msg ssm left join shequ.sqfw_form_summary sfs " +
                "on ssm.summary_id = sfs.id " +
                "where sfs.sqfw_id = '"+sqfwId+"' order by create_date desc";
        List<Sqfw_Sms_Msg> smsMsgList = this.listEntity(Sqls.create(sql));
        if (smsMsgList == null) return 0;
        return smsMsgList.size();
    }
}
